<template>
	<view class="page-fade-in" :style="{opacity: pageOpc}">
		<template v-if="pageOpc">
			<view class="content">
				<view class="manage" @tap="changeManage">{{ismManage?'退出管理':'管理'}}</view>
				<view class="shop-cont"  v-for="(item,index) in cartList" :key="index" @tap="cartCheck(index)">
					<image src="../../static/order/select-icon.png" class="select-icon" v-if="item.check"></image>
					<image src="../../static/order/notselect-icon.png" class="select-icon" v-else></image>
					<view class="shop-item flexWrapNo">
						<image :src="item.goods.image" class="shop-img"></image>
						<view class="shop-info-box flexWarpColumn">
							<view class="info-box">
								<view>{{item.goods.title}}</view>
								<view class="specs-box">{{item.sku_attr||'默认'}}</view>
							</view>
							<view class="operation-box flexWrapNo">
								<view class="reduce" @tap.stop="reduce(index)">-</view>
								<view class="num">{{item.nums}}</view>
								<view class="plus" @tap.stop="plus(index)">+</view>
							</view>
							<view class="price">¥{{item.sku.price||item.goods.price}}</view>
						</view>
					</view>
					<image src="../../static/shop/del-icon.png" class="del-icon" @tap.stop="delCart(index,item.id)" v-if="ismManage"></image>
					<view class="total-box">
						共{{item.nums}}件 总额 <text class="price">¥ {{item.subtotal}}</text>
					</view>
				</view>
				<view class="empty-box" v-if="cartList.length==0">
					<image src="https://static.mo-yu.vip/wechat/images/orderEmpty.png"></image>
				</view>
				<view class="bottom-box flexWrap">
					<view class="total-box flexWrapNo" @tap="checkAll()">
						<image src="../../static/order/select-icon.png" class="select-icon" v-if="allCheck"></image>
						<image src="../../static/order/notselect-icon.png" class="select-icon" v-else></image>
						全选
						<view class="total-money" v-if="!ismManage">合计: <text>￥{{totalPrice}}</text></view>
					</view>
					<view class="define-btn" @tap="handle()" :class="{deleteBtn:ismManage}">{{ismManage?'删除':'结算'}}</view>
				</view>
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageOpc:0,
				cartList:[],
				allCheck:false,
				ismManage:false
			}
		},
		onLoad() {
			this.getCartList();
		},
		onReady(){
			setTimeout(() => {
				this.pageOpc=1
			}, 100)
		},
		computed:{
			// 计算购物车商品价格
			totalPrice(){
				let that=this
				let totalPrice=0;
				for(let i in this.cartList){
					if(this.cartList[i].check){
						totalPrice+=Number(this.cartList[i].subtotal)
					}
				}
				return totalPrice.toFixed(2);
			},
		},
		methods: {
			reduce(index) {
				if (this.cartList[index].nums == 1) return;
				this.cartList[index].nums--
				this.setNums(index)
			},
			plus(index) {
				let stocks=0
				if(this.cartList[index].sku==null){
					stocks=this.cartList[index].goods.stocks
				}else{
					stocks=this.cartList[index].sku.stocks
				}
				console.log(stocks)
				if(this.cartList[index].nums==stocks){
					this.$tools.showToast('库存不足')
					return;
				}
				this.cartList[index].nums++
				this.setNums(index)
			},
			setNums(index){
				this.$request.post(this.$api.set_nums, {
					nums:this.cartList[index].nums,
					id:this.cartList[index].id
				}, {
					token:true
				}).then(res => {
					if(res.code==1){
						let price=0
						if(this.cartList[index].sku==null){
							price=this.cartList[index].goods.price
						}else{
							price=this.cartList[index].sku.price
						}
						this.cartList[index].subtotal=(this.cartList[index].nums*price).toFixed(2);
					}
				})
			},
			getCartList(){
				this.$request.post(this.$api.cartList, {
					store_id:uni.getStorageSync('store_id')
				}, {
					token:true
				}).then(res => {
					let {
						data = {}
					} = res;
					for (let i in data) {
						data[i].check=false
					}
					this.cartList=data
				})
			},
			checkAll(){
				this.allCheck=!this.allCheck
				for (let i in this.cartList) {
					this.cartList[i].check=this.allCheck
				}
			},
			changeManage(){
				this.ismManage=!this.ismManage
			},
			cartCheck(index){
				this.cartList[index].check=!this.cartList[index].check
			},
			delCart(index,cart_id){
				if(index>-1){
					this.cartList.splice(index,1)
					console.log(index)
				}
				this.$request.post(this.$api.cartDel, {
					store_id:uni.getStorageSync('store_id'),
					ids:cart_id
				}, {
					token:true
				}).then(res => {
					this.$tools.showToast('删除成功')
				})
			},
			handle(){
				let indices = this.cartList.filter(element => element.check)
				console.log(indices);
				if(!indices.length){
					this.$tools.showToast('请先选择商品')
					return;
				}
				let cart_id=indices.map(item=>item.id).join(',')
				if(this.ismManage){
					uni.showModal({
						title:'确认删除商品',
						confirmText:'删除',
						confirmColor:"#fd2626",
						success: (res) => {
							if(res.confirm){
								let result=this.cartList.filter(item => !indices.includes(item))
								this.cartList=result
								this.delCart(-1,cart_id)
							}
						}
					})
				}else{
					this.goPage('/pages/shop/placeOrder?cart_id='+cart_id)
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		padding-bottom: 200upx;
	}
	.content{
		padding: 30upx;
	}
	.manage{
		font-size: 28upx;
		color: #FFFFFF;
		margin-bottom: 30upx;
		text-align: end;
	}
	.shop-cont{
		padding: 32upx 28upx;
		padding-top: 60upx;
		padding-left: 90upx;
		background: #1D1C5C;
		border-radius: 16rpx;
		margin-bottom: 30upx;
		position: relative;
		.select-icon{
			width: 50upx;
			height: 50upx;
			position: absolute;
			top: 30upx;
			left: 20upx;
		}
		.del-icon{
			width: 50upx;
			height: 50upx;
			position: absolute;
			bottom: 130upx;
			right: 20upx;
		}
		.shop-item{
			border-bottom: 1rpx solid #36355E;
			padding-bottom: 30upx;
			.shop-img{
				width: 184rpx;
				height: 184rpx;
				border-radius: 16rpx 16rpx 16rpx 16rpx;
				flex: none;
			}
			.shop-info-box{
				margin-left: 28upx;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color:#FFFFFF;
				height: 184rpx;
				justify-content: space-between;
				flex: auto;
				.info-box{
					.specs-box{
						color: #8786A8;
						margin-top: 12upx;
					}
				}
				.operation-box {
					.reduce {
						width: 40rpx;
						height: 40rpx;
						background: #26BCFD;
						border-radius: 8rpx;
						font-size: 36rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
						line-height: 40rpx;
						text-align: center;
					}
				
					.num {
						margin: 0 40upx;
						font-size: 30rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
					}
				
					.plus {
						width: 40rpx;
						height: 40rpx;
						background: #26BCFD;
						border-radius: 8rpx;
						font-size: 36rpx;
						font-family: PingFang SC-Medium, PingFang SC;
						font-weight: 500;
						color: #FFFFFF;
						line-height: 40rpx;
						text-align: center;
					}
				}
			}
		}
		.total-box{
			font-size: 28rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 400;
			color:#8786A8;
			line-height: 30upx;
			text-align: right;
			padding: 0 28upx;
			margin-top: 20upx;
			.price{
				color: #FFFFFF;
				margin-left: 24upx;
			}
		}
	}
	.bottom-box {
		height: 130rpx;
		background: #1D1C5C;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		margin: auto;
		font-size: 32rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		padding: 0 30upx;
		.total-box{
			.select-icon{
				width: 50upx;
				height: 50upx;
				margin-right: 20upx;
			}
			.total-money{
				margin-left: 20upx;
				font-size: 28upx;
				text{
					font-size: 30upx;
					margin-left: 20upx;
				}
			}
		}
		.define-btn {
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 80rpx;
			width: 192rpx;
			height: 80rpx;
			background: #26BCFD;
			border-radius: 56rpx;
			text-align: center;
		}
		.deleteBtn{
			background: #fd2626;
		}
	}
</style>
